package com.utils.protobuffer;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//
//import com.kyhd.bidder.controller.Model.CallResult;
//import com.kyhd.bidder.service.adver.TbAdverService;
//import com.kyhd.bidder.util.AdxConst;
//
//import cpc_rta.CpcRta;
//import cpc_rta.CpcRta.Device;
//import cpc_rta.CpcRta.DeviceId;
//import cpc_rta.CpcRta.DeviceIdType;
//import cpc_rta.CpcRta.RTAResponse;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//
//@Api("淘宝接口")
////@RestController
//@Controller
//@RequestMapping(value="/taobao/api")
//@Slf4j
//public class TaobaoController {
//
//	@Autowired
//	TbAdverService tbAdverService;
//	
//	@ApiOperation(value="趣头条拉活淘宝", notes="")
////	@RequestMapping(value = "qutoutiao", produces = "application/x-protobuf")
//	@RequestMapping(method = {RequestMethod.POST}, value="qutoutiao", produces = "application/x-protobuf")
//	public HttpServletResponse qutoutiao(HttpServletRequest request, HttpServletResponse response) {
//		response.setContentType("application/x-protobuf;charset:utf-8");
//      InputStream in = null;
//		OutputStream ops = null;
//		try {
//			in = request.getInputStream();
//			byte[] bytesRe = toByteArray(in);
//			CpcRta.RTARequest ctaReq = CpcRta.RTARequest.parseFrom(bytesRe);
//			log.info("taobao-qutoutiao request:{}",ctaReq);
//			
//			CallResult result = null;
//			long currentTimeMillisStart = System.currentTimeMillis();
//			Device device = ctaReq.getDevice();
//			if(device.getOs().getNumber() == 1 ) {
//				DeviceId deviceId = device.getDevId();
//					deviceId.getType();
//					if(DeviceIdType.IMEI_VALUE == 1) {
//						if(deviceId.getIsMd5()) {
//							result = TbAdverService.judgePixelAlive(
//									AdxConst.qutoutiaoADX, deviceId.getId(), null,
//									TbAdverService.ALIVE_CHANNEL_CODE, TbAdverService.qtt_alive_ADID, null
//									, null);
//						}
//				}
//			}else if(device.getOs().getNumber() == 2) {
//					DeviceId deviceId = device.getDevId();
//					deviceId.getType();
//					if(DeviceIdType.IDFA_VALUE == 2) {
//						if(deviceId.getIsMd5()) {
//							result = TbAdverService.judgePixelAlive(
//									AdxConst.qutoutiaoADX, null, deviceId.getId(),
//									TbAdverService.ALIVE_CHANNEL_CODE, TbAdverService.qtt_alive_ADID, null
//									, null);
//						}
//				}
//			}
//			log.info("taobao-qutoutiao aliPayUserQuery:{}",result);
//			
//			RTAResponse req = null;
//			
//			CpcRta.RTAResponse.Builder ResponseBuilder = CpcRta.RTAResponse.newBuilder();
//			if(result.getCode() == 2) {
//				ResponseBuilder.setStatusCodeValue(0);
//				long currentTimeMillisEnd = System.currentTimeMillis();
//				ResponseBuilder.setCostTime(currentTimeMillisEnd-currentTimeMillisStart);
//				req = ResponseBuilder.build();
//				log.info("taobao-qutoutiao response:{}",response);
//			}else {
//				ResponseBuilder.setStatusCodeValue(1);
//				long currentTimeMillisEnd = System.currentTimeMillis();
//				ResponseBuilder.setCostTime(currentTimeMillisEnd-currentTimeMillisStart);
//				req = ResponseBuilder.build();
//				log.info("taobao-qutoutiao response:{}",response);
//			}
//			
//			ops = response.getOutputStream();
//			ops.write(req.toByteArray());
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}finally {
//			try {
//              if (in != null) 
//                  in.close();
//              if(ops!=null) 
//					ops.close();
//          } catch (IOException ex) {
//              ex.printStackTrace();
//          }
//		}
//		return response;
//	}
//	
//	
//	 public static byte[] toByteArray(InputStream input) throws IOException {
//	        ByteArrayOutputStream output = new ByteArrayOutputStream();
//	        byte[] buffer = new byte[4096];
//	        int n = 0;
//	        while (-1 != (n = input.read(buffer))) {
//	            output.write(buffer, 0, n);
//	        }
//	        return output.toByteArray();
//	 }
//}
